﻿/*******************************************************************************
#                    ExpertBroker.cs
#  Comment:
#                    
#
#  Current Version: V1.0
#  Author: 
#
#  History List:
#  V1.0    Created by CodeBuilder@2009-44-16
#
#******************************************************************************/

using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using SYS.Basic;

using ExpertMIS.Brokers;
using ExpertMIS.Entities;
using System.Collections;

namespace ExpertMIS.Brokers
{
    public class ExpertBroker : ExpertMISBaseBroker
    {
        #region Private Methods

        private object UpdateExpertEntity(IEntity entity)
        {
            return SqlMap.Update("UpdateExpertEntity", entity);
        }

        private object InsertExpertEntity(IEntity entity)
        {
            return SqlMap.Insert("InsertExpertEntity", entity);
        }

        private object DeleteExpertEntity(IEntity entity)
        {
            return SqlMap.Delete("DeleteExpertEntity", entity);
        }

        #endregion

        #region Override Methods

        protected override IEntity ProcessGetEntity(System.Collections.Hashtable keys)
        {
            if (!keys.Contains("OID")) throw new Exception("无效的请求参数。");
            return SqlMap.QueryForObject<IEntity>("SelectExpertEntityByOID", keys["OID"] as string);
        }

        protected override bool ProcessSaveEntity(IEntity entity, Hashtable request)
        {
            try
            {
                ExpertEntity expert = entity as ExpertEntity;

                SqlMap.BeginTransaction();
                if (entity.EntityState == EntityState.New)
                {
                    InsertExpertEntity(entity);
                    entity.EntityState = EntityState.Old;
                }
                else
                {
                    UpdateExpertEntity(entity);
                }

                List<string> sDomains = request["domains"] as List<string>;

                if (sDomains != null)
                {
                    ExpertBelongDomainBroker broker = new ExpertBelongDomainBroker();
                    List<ExpertBelongDomainEntity> domainList = new List<ExpertBelongDomainEntity>();
                    foreach (string s in sDomains)
                    {
                        ExpertBelongDomainEntity domainEntity = broker.NewEntity() as ExpertBelongDomainEntity;
                        domainEntity.DomainOID = s;
                        domainEntity.ExpertOID = expert.OID;
                        domainList.Add(domainEntity);
                    }

                    broker.UpdateExpertDomains(expert.OID, domainList);
                }

                SqlMap.CommitTransaction();
            }
            catch (Exception ex)
            {
                SqlMap.RollBackTransaction();
                Logger.Error(GetType().ToString() + ".ProcessSaveEntity", ex);
            }

            return true;
        }

        protected override IEntity ProcessNewEntity()
        {
            ExpertEntity entity = new ExpertEntity();

            entity.CreatTime = DateTime.Now;
            entity.LastUpdateDate = DateTime.Now;

            return entity;
        }

        protected override bool ProcessRemoveEntity(IEntity entity, Hashtable request)
        {
            try
            {
                DeleteExpertEntity(entity);
                return true;
            }
            catch
            {
                return false;
            }
        }

        #endregion

        #region Public Method

        /// <summary>
        /// 获取推荐专家列表
        /// </summary>
        /// <param name="count">返回条数</param>
        /// <param name="isByDoMain">是否按不同领域获取数据</param>
        /// <returns></returns>
        public IList<ExpertEntity> GetReCommendEperts(int count, string doMainID)
        {
            return SqlMap.QueryForList<ExpertEntity>("GetReCommendEperts", string.Format(" top {0} ", count));
        }

        public IList<ExpertEntity> GetHotNExperts(int count)
        {
            return SqlMap.QueryForList<ExpertEntity>("GetHotNExperts", string.Format(" top {0} ", count));
        }

        public IList<ExpertEntity> GetNewNExperts(int count)
        {
            return SqlMap.QueryForList<ExpertEntity>("GetNewNExperts", string.Format(" top {0} ", count));
        }

        public IList<ExpertEntity> GetExpertsByDomain(IList<string> listDomain)
        {
            if (listDomain == null || listDomain.Count <= 0)
            {
                return null;
            }

            try
            {
                string domains = string.Empty;
                foreach (string s in listDomain)
                {
                    if (!string.IsNullOrEmpty(domains)) domains += ",";
                    domains += string.Format("'{0}'", s);
                }

                return SqlMap.QueryForList<ExpertEntity>("SelectExpertsByDomain", domains);
            }
            catch (Exception ex)
            {
                return null;
            }
        }

        public IList<ExpertEntity> GetExpertsByDomain(string domainOID)
        {
            List<string> listDomain = new List<string>();
            listDomain.Add(domainOID);
            return GetExpertsByDomain(listDomain);
        }

        /// <summary>
        /// 获取未注册用户信息的专家列表
        /// </summary>
        /// <returns></returns>
        public IList<ExpertEntity> GetUnRegisterExperts()
        {
            return SqlMap.QueryForList<ExpertEntity>("SelectUnRegisterEperts", null);
        }

        public bool UpdateExpertUserID(string ExpertID, string p)
        {
            try
            {
                if (string.IsNullOrEmpty(ExpertID) || string.IsNullOrEmpty(p))
                {
                    return false;
                }

                Hashtable hsUpdate = new Hashtable();
                hsUpdate.Add("OID", ExpertID);
                hsUpdate.Add("UserID", p);

                SqlMap.Update("UpdateExpertUserID", hsUpdate);
                return true;
            }
            catch
            {
                return false;
            }
        }

        public ExpertEntity GetExpertEntityByUserID(string UserID)
        {
            return SqlMap.QueryForObject<ExpertEntity>("SelectExpertByUserID", UserID);
        }

        public IList<DomainEntity> GetExpertDomains(string ExpertOID)
        {
            ExpertBelongDomainBroker broker = new ExpertBelongDomainBroker();
            DomainBroker domainBroker = new DomainBroker();

            List<DomainEntity> returnList = new List<DomainEntity>();

            foreach(ExpertBelongDomainEntity relateDomain in broker.GetDomainByExpertOID(ExpertOID))
            {
                Hashtable htKeys = new Hashtable();
                htKeys["OID"] = relateDomain.DomainOID;
                DomainEntity domainEntity = domainBroker.GetEntity(htKeys) as DomainEntity;
                returnList.Add(domainEntity);
            }

            return returnList as IList<DomainEntity>;
        }

        public void UpdateExpertTotalGrade(string ExpertOID, decimal Grade)
        {
            Hashtable htKeys = new Hashtable();
            htKeys["ExpertOID"] = ExpertOID;
            htKeys["Grade"] = Grade;
            SqlMap.Update("UpdateExpertTotalGrade", htKeys);
        }

        public void UpdateExpertTotalGrade(Dictionary<string, decimal> expertGrade)
        {
            try
            {
                SqlMap.BeginTransaction();
                
                foreach (string expertOID in expertGrade.Keys)
                {
                    UpdateExpertTotalGrade(expertOID, expertGrade[expertOID]);
                }

                SqlMap.CommitTransaction();
            }
            catch(Exception ex)
            {
                SqlMap.RollBackTransaction();
                Logger.Error("更新专家分值时出错", ex);
            }
        }

        /// <summary>
        /// 根据专家ID，获取专家信息
        /// </summary>
        /// <param name="OID"></param>
        /// <returns></returns>
        public ExpertEntity GetExpertByOID(string OID)
        {
            return SqlMap.QueryForObject<ExpertEntity>("SelectExpertEntityByOID", OID);
        }

        public int GetExpertCountByDomainOID(string DomainOID)
        {
            return SqlMap.QueryForObject<int>("GetDomainExpertCount", DomainOID);
        }

        public IList<ExpertEntity> GetDomainRecommandExperts(string DomainOID, int Count)
        {
            Hashtable htKeys = new Hashtable();
            if (Count <= 0)
            {
                htKeys["PREFIX"] = " * ";
            }
            else
            {
                htKeys["PREFIX"] = string.Format(" TOP {0} * ",Count);
            }
            htKeys["DomainOID"] = DomainOID;

            return SqlMap.QueryForList<ExpertEntity>("GetDomainRecommandExperts", htKeys);
        }

        #endregion
    }
}